<?php
function buildWhere($filter = '') {
	$where = " 0 = 0 ";
	if (is_array($filter)) {
	for ($i=0;$i < count($filter);$i++){
		switch($filter[$i]['data']['type']){

		case 'string' : 
			switch ($filter[$i]['data']['comparison']) {
				case 'eq': $qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
					break;
				case 'ne': $qs .= " AND ".$filter[$i]['field']." != '".$filter[$i]['data']['value']."'";
					break;
				case 'list':
					if(strstr($filter[$i]['data']['value'],',')){
						$fi = explode(',',$filter[$i]['data']['value']);
						for ($q=0;$q<count($fi);$q++){
							$fi[$q] = "'".$fi[$q]."'";
						}
						$filter[$i]['data']['value'] = implode(',',$fi);
						$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
					}else{
						$qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
					}
					break;
				case 'slk': $qs .= " AND ".$filter[$i]['field']." LIKE '%".$filter[$i]['data']['value']."'";
					break;
				case 'elk': $qs .= " AND ".$filter[$i]['field']." LIKE '".$filter[$i]['data']['value']."%'";
					break;
				case 'lk': $qs .= " AND ".$filter[$i]['field']." LIKE '%".$filter[$i]['data']['value']."%'";
					break;
			}
			break;
		case 'numeric':
			switch ($filter[$i]['data']['comparison']) {
				case 'eq' : $qs .= " AND ".$filter[$i]['field']." = ".$filter[$i]['data']['value'];
					break;
				case 'ne' : $qs .= " AND ".$filter[$i]['field']." != ".$filter[$i]['data']['value'];
					break;
				case 'lt' : $qs .= " AND ".$filter[$i]['field']." < ".$filter[$i]['data']['value'];
					break;
				case 'gt' : $qs .= " AND ".$filter[$i]['field']." > ".$filter[$i]['data']['value'];
					break;
				case 'lte' : $qs .= " AND ".$filter[$i]['field']." <= ".$filter[$i]['data']['value'];
					break;
				case 'gte' : $qs .= " AND ".$filter[$i]['field']." >= ".$filter[$i]['data']['value'];
					break;
				case 'list':
					if(strstr($filter[$i]['data']['value'],',')){
						$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
					}else{
						$qs .= " AND ".$filter[$i]['field']." = ".$filter[$i]['data']['value'];
					}
					break;
			}
			break;
		case 'boolean' : $qs .= " AND ".$filter[$i]['field']." = ".($filter[$i]['data']['value']);
			break;
		case 'date' :
			switch ($filter[$i]['data']['comparison']) {
				case 'eq' : $qs .= " AND ".$filter[$i]['field']." = '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'ne' : $qs .= " AND ".$filter[$i]['field']." != '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'lt' : $qs .= " AND ".$filter[$i]['field']." < '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'gt' : $qs .= " AND ".$filter[$i]['field']." > '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'lte' : $qs .= " AND ".$filter[$i]['field']." <= '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
				case 'gte' : $qs .= " AND ".$filter[$i]['field']." >= '". @date('Y-m-d',strtotime($filter[$i]['data']['value']))."'";
					break;
			}
			break;
		case 'list'   :
			if (strstr($filter[$i]['data']['value'],',')){
			$fi = explode(',',$filter[$i]['data']['value']);
			for ($q=0;$q<count($fi);$q++){
				$fi[$q] = "'".$fi[$q]."'";
			}
			$filter[$i]['data']['value'] = implode(',',$fi);
			$qs .= " AND ".$filter[$i]['field']." IN (".$filter[$i]['data']['value'].")";
			}else{
			$qs .= " AND ".$filter[$i]['field']." = '".$filter[$i]['data']['value']."'";
			}
			break;
		} # switch
	} # for
	$where .= $qs;
	} # if
        return $where;
}
function build_order( $tokens ) {

	if(count($tokens) && is_array($tokens)){
		$qq = array();
		foreach($tokens as $token) {
			$qq[] = $token['field'] . " " . $token['dir'];
		}

		return (' ORDER BY ' . implode(", ", $qq) );
	}

}

/**
 *
 */
function build_limit( $start = '', $range = '' ) {
	$str = " LIMIT ";
	if ($range != '') {
		if ($start !== '' && $start != NULL) {
			$str .= $start . ', ';
		}
		return ($str . $range);
	}
	
}

function getCondition($condition, $value){
	$str = "";
	switch($condition){
		case 'LIKE_RIGHT' : 
			$str = " LIKE '{$value}%' ";
			break;
		case 'LIKE_LEFT' : 
			$str = " LIKE '%{$value}' ";
			break;
		case 'LIKE_BOTH' : 
			$str = " LIKE '%{$value}%' ";
			break;
		case 'EQUAL' : 
			$str = " = '{$value}' ";
			break;
		case 'NOT_EQUAL' : 
			$str = " != '{$value}'";
			break;
		case 'IN' : 
			$str = " IN ({$value}) ";
			break;
		DEFAULT :
			$str = false;
	}
	return $str;
}

function getError($code) {
	$ar = array(
		  101 => 'Term should be given.'
		, 102 => 'Association should be given.'
		);
	return($ar[$code]);
}
?>